﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlackjackSimulator
{
    public class Game
    {
        private MyPlayer _me;

        public MyPlayer Me
        {
            get { return _me; }
            set { _me = value; }
        }

        private CardPile _cardPile;

        public CardPile CardPile
        {
            get { return _cardPile; }
            set { _cardPile = value; }
        }
        private int _numDecks;

        public int NumDecks
        {
            get { return _numDecks; }
            set { _numDecks = value; }
        }

        private List<OtherPlayer> _otherPlayer;

        public List<OtherPlayer> OtherPlayer
        {
            get { return _otherPlayer; }
            set { _otherPlayer = value; }
        }
        private int _numOtherPlayer;

        public int NumOtherPlayer
        {
            get { return _numOtherPlayer; }
            set { _numOtherPlayer = value; }
        }
        private int _moneyBet;

        public int MoneyBet
        {
            get { return _moneyBet; }
            set { _moneyBet = value; }
        }
        private Dealer _cardDealer;

        public Dealer CardDealer
        {
            get { return _cardDealer; }
            set { _cardDealer = value; }
        }
        public Game(int moneyBet, int numDecks, int numPlayers)
        {
            _moneyBet = moneyBet;
            _numDecks = numDecks;
            _cardDealer = new Dealer();
            _numOtherPlayer = numPlayers;
            _otherPlayer = new List<OtherPlayer>();
            for (int i = 0; i < numPlayers - 1; i++)
            {
                OtherPlayer temp = new OtherPlayer();
                temp.Money = 100;
                _otherPlayer.Add(temp);
            }
            
         
            _me = new MyPlayer();
            _me.Money = 100;
        }

        private void reset()
        {
            _me.Reset();
            for (int i = 0; i < _numOtherPlayer - 1; i++)
                _otherPlayer[i].Reset();
            _cardDealer.Reset();
        }

        public void Start(int moneyBet)
        {
            _moneyBet = moneyBet;
            reset();
            _cardPile = new CardPile(_numDecks);
            _cardPile.Shuffle();
            DealCard();
        }
        public Card DealOneCard()
        {
            return CardPile.DealOneCard();
        }
        public void DealCard()
        {
            for (int i = 0; i < 2; i++)
            {
                _me.Hand1.Add(DealOneCard());
                for (int j = 0; j < _otherPlayer.Count; j++)
                {
                    _otherPlayer[j].Hand1.Add(DealOneCard());
                }
                _cardDealer.Hand.Add(DealOneCard());
            }
        }
        public void processPlayer(int index)
        {
            if (OtherPlayer[index].Hand1.isBlackJack() == true)
                return;
            
            int point = OtherPlayer[index].Hand1.CountPoint();
            if (point == 10 || point == 11)
            {
                OtherPlayer[index].Hand1.IsDoubleDown = true;
                OtherPlayer[index].Hand1.Add(DealOneCard());
                OtherPlayer[index].Hand1.CheckBust();
                if (OtherPlayer[index].Hand1.IsBust == true)
                {
                    OtherPlayer[index].Money -= MoneyBet * 2;
                }
                return;
            }
            while (point <= 17)
            {
                OtherPlayer[index].Hand1.Add(DealOneCard());
                OtherPlayer[index].Hand1.CheckBust();
                point = this.CardDealer.Hand.CountPoint();
                if (OtherPlayer[index].Hand1.IsBust == true)
                {
                    OtherPlayer[index].Money -= MoneyBet;
                    return;
                }
                
            }
            OtherPlayer[index].Hand1.IsStand = true;

        }
        public void DealerPlay()
        {
            if (this.CardDealer.Hand.isBlackJack() == true)
                return;

            int point = this.CardDealer.Hand.CountPoint();
            while (point <= 17)
            {
                this.CardDealer.Hand.Add(DealOneCard());
                this.CardDealer.Hand.CheckBust();
                point = this.CardDealer.Hand.CountPoint();
                if (this.CardDealer.Hand.IsBust == true)
                {
                    return;
                }

            }

        }

        public void AutomaticPlay()
        {
            for (int i = 0; i < OtherPlayer.Count; i++)
            {
                processPlayer(i);
            }
            DealerPlay();
            EndGame();
        }
        public void EndGame()
        {
            #region process Me
            if (_me.Hand1.IsBust == false)
                {
                    if (_cardDealer.Hand.IsBust == true)
                    {
                        if (_me.Hand1.IsDoubleDown == true)
                            _me.Money += _moneyBet * 2;
                        else
                            _me.Money += _moneyBet;
                        _me.IsWin = true;
                    }
                    else
                    {
                        if (_cardDealer.Hand.CountPoint() < _me.Hand1.CountPoint())
                        {
                            if (_me.Hand1.IsDoubleDown == true)
                                _me.Money += _moneyBet * 2;
                            else
                                _me.Money += _moneyBet;
                            _me.IsWin = true;
                        }
                        else if (_cardDealer.Hand.CountPoint() > _me.Hand1.CountPoint())
                        {
                            if (_me.Hand1.IsDoubleDown == true)
                                _me.Money -= _moneyBet * 2;
                            else
                                _me.Money -= _moneyBet;
                            _me.IsWin = false;
                        }
                    }
                }
                if (_me.Hand1.IsSplit == true)
                {
                    if (_me.Hand2.IsBust == false)
                    {
                        if (_cardDealer.Hand.IsBust == true)
                        {
                            if (_me.Hand2.IsDoubleDown == true)
                                _me.Money += _moneyBet * 2;
                            else
                                _me.Money += _moneyBet;
                            _me.IsWin = true;
                        }
                        else
                        {
                            if (_cardDealer.Hand.CountPoint() < _me.Hand2.CountPoint())
                            {
                                if (_me.Hand2.IsDoubleDown == true)
                                    _me.Money += _moneyBet * 2;
                                else
                                    _me.Money += _moneyBet;
                                _me.IsWin = true;
                            }
                            else if (_cardDealer.Hand.CountPoint() > _me.Hand2.CountPoint())
                            {
                                if (_me.Hand2.IsDoubleDown == true)
                                    _me.Money -= _moneyBet * 2;
                                else
                                    _me.Money -= _moneyBet;
                                _me.IsWin = false;
                            }
                        }
                    }
                }   
#endregion
            #region otherplayer
                for (int i = 0; i < OtherPlayer.Count; i++)
                {
                    EndPlayer(i);
                }
            #endregion

        }
        public void EndPlayer(int index)
        {
            if (_otherPlayer[index].Hand1.IsBust == false)
            {
                if (_cardDealer.Hand.IsBust == true)
                {
                    if (_otherPlayer[index].Hand1.IsDoubleDown == true)
                        _otherPlayer[index].Money += _moneyBet * 2;
                    else
                        _otherPlayer[index].Money += _moneyBet;
                    _otherPlayer[index].IsWin = true;
                }
                else
                {
                    if (_cardDealer.Hand.CountPoint() < _otherPlayer[index].Hand1.CountPoint())
                    {
                        if (_otherPlayer[index].Hand1.IsDoubleDown == true)
                            _otherPlayer[index].Money += _moneyBet * 2;
                        else
                            _otherPlayer[index].Money += _moneyBet;
                        _otherPlayer[index].IsWin = true;
                    }
                    else if (_cardDealer.Hand.CountPoint() > _otherPlayer[index].Hand1.CountPoint())
                    {
                        if (_otherPlayer[index].Hand1.IsDoubleDown == true)
                            _otherPlayer[index].Money -= _moneyBet * 2;
                        else
                            _otherPlayer[index].Money -= _moneyBet;
                        _otherPlayer[index].IsWin = false;
                    }
                }
            }
        }
    }
}
